#include <QMessageBox>
#include <QPainter>
#include <QDebug>

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog.h"
#include "bdialog.h"
#include "graphalgorithm.h"
#include "Bezier.h"

int Tx, Ty;
double Sx, Sy, R;
QPoint pR;
//int Fac(int n);
//float Power(float f, int n);
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
//    qDebug() << Fac(5);
//    qDebug() << Power(2.1, 10);
    state = 0x3f;
    m_GI.color = qRgb(255, 0, 0);
    palette = new  QImage(800, 600, QImage::Format_RGB32);
    bg = new QImage(this->size(), QImage::Format_RGB32);

    bshEllipse(bg, QPoint(5, 10), QSize(2, 4), qRgb(0, 255, 0));
    DDA(bg, QPoint(10, 10), QPoint(10, 560), qRgb(0, 255, 0));
    DDA(bg, QPoint(5, 555), QPoint(10, 560), qRgb(0, 255, 0));
    DDA(bg, QPoint(15, 555), QPoint(10, 560), qRgb(0, 255, 0));
    DDA(bg, QPoint(10, 10), QPoint(760, 10), qRgb(0, 255, 0));
    DDA(bg, QPoint(755, 5), QPoint(760, 10), qRgb(0, 255, 0));
    DDA(bg, QPoint(755, 15), QPoint(760, 10), qRgb(0, 255, 0));

    connect(ui->actionline, &QAction::triggered, this, &MainWindow::Line);
    connect(ui->actionTri, &QAction::triggered, this, &MainWindow::Poly3);
    connect(ui->actionSix, &QAction::triggered, this, &MainWindow::Poly6);
    connect(ui->actionEllipse, &QAction::triggered, this, &MainWindow::Ellipse);
    connect(ui->actionBezier, &QAction::triggered, this, &MainWindow::BezierLine);
    connect(ui->actionBsp, &QAction::triggered, this, &MainWindow::BspLine);

    connect(ui->actionTrans, &QAction::triggered, this, &MainWindow::Trans);
    connect(ui->actionRotate, &QAction::triggered, this, &MainWindow::Rotate);
    connect(ui->actionScale, &QAction::triggered, this, &MainWindow::Scale);

    connect(ui->actionFill, &QAction::triggered, this, &MainWindow::Fill);
    connect(ui->actionClip, &QAction::triggered, this, &MainWindow::Clip);
}

MainWindow::~MainWindow()
{
    delete ui;
    delete bg;
    delete palette;
}


void MainWindow::paintEvent(QPaintEvent *)
{
/*    if(state == bezierL){
        QPainter painter(palette);
        DrawBezier(painter, m_GI);
      } //*/
/*   if(state == bspL){
        QPainter painter(palette);
        DrawBsp(painter, m_GI);
      } //*/
    QPainter painter(bg);
    painter.setCompositionMode(QPainter::CompositionMode_Screen);
    painter.drawImage(10, 10, *palette);
    painter.end();

    painter.begin(this);
    painter.drawImage(0, 30, *bg);
}

void MainWindow::Line()
{
    Dialog d(gline, &m_GI);
    if(d.exec() == QDialog::Accepted){
        Reset();//qDebug()<<m_GI.points[0];
        DDA(palette, m_GI.points[0], m_GI.points[1], m_GI.color);
        repaint();
    }
    state = gline;
}
void MainWindow::Ellipse()
{
    Dialog d(ellipse, &m_GI);
    if(d.exec() == QDialog::Accepted){
        Reset();
        bshEllipse(palette, m_GI.points[0], m_GI.Esize, m_GI.color);
        repaint();
    }
    state = ellipse;
}

void MainWindow::BezierLine()
{
    BDialog d(bezierL, &m_GI);
    if(d.exec() == QDialog::Accepted){
        Reset();
        DrawBezier(palette, m_GI.ctrlPoint, m_GI.color);
        state = bezierL;
        repaint();
    }
}

void MainWindow::BspLine()
{
    BDialog d(bspL, &m_GI);
    if(d.exec() == QDialog::Accepted){
        Reset();
        DrawBsp(palette, m_GI.ctrlPoint, m_GI.color, m_GI.B_part);
        state = bspL;
        repaint();
    }
}
void MainWindow::Poly6()
{
    Dialog d(polygon6, &m_GI);
    if(d.exec() == QDialog::Accepted){
        Reset();
        Polygon6(palette, m_GI.points[0].x(), m_GI.points[0].y(), m_GI.Esize.width(), m_GI, m_GI.color);
        repaint();
    }
    state = polygon6;
}
void MainWindow::Poly3()
{
    Dialog d(polygon3, &m_GI);
    if(d.exec() == QDialog::Accepted){
        Reset();
        Polygon3(palette, m_GI.points[0], m_GI.points[1], m_GI.points[2], m_GI.color);
        repaint();
    }
    state = polygon3;
}

void MainWindow::Trans()
{
    Dialog d(OP_TRANS);
    if(d.exec() == QDialog::Accepted){
        Reset();
        trans(palette, Tx, Ty, state, m_GI);
        repaint();
    }
}

void MainWindow::Rotate()
{
    Dialog d(OP_ROTATE);
    if(d.exec() == QDialog::Accepted){
        Reset();
        rotate(palette, R, pR, state, m_GI);
        repaint();
    }
}

void MainWindow::Scale()
{
    Dialog d(OP_SCALE);
    if(d.exec() == QDialog::Accepted){
        Reset();
        scale(palette, Sx, Sy, state, m_GI);
        repaint();
    }
}

void MainWindow::Fill()
{
    QColor color = QColorDialog::getColor();
    m_GI.color = color.rgb();
    scanLineFill(palette, m_GI.points, m_GI.color, state);
    repaint();
}

void MainWindow::Clip()
{
    Dialog d(OP_CLIP, &m_GI);
    if(d.exec() == QDialog::Accepted){
        Reset();
        int x1 = m_GI.points[0].x(), y1 = m_GI.points[0].y();
        int x2 = m_GI.points[1].x(), y2 = m_GI.points[1].y();qDebug() << x1 << "," << y1 <<","<< x2<<","<<y2;
        CohenSutherlandClip(palette, QRect(pR, m_GI.Esize), x1, y1, x2, y2, m_GI.color);//qDebug() << "22";
        repaint();
    }
}

void MainWindow::Reset(){
    delete palette;
    palette = new QImage(800, 600, QImage::Format_RGB32);

    delete bg;
    bg = new QImage(this->size(), QImage::Format_RGB32);
    bshEllipse(bg, QPoint(5, 10), QSize(2, 4), qRgb(0, 255, 0));
    DDA(bg, QPoint(10, 10), QPoint(10, 560), qRgb(0, 255, 0));
    DDA(bg, QPoint(5, 555), QPoint(10, 560), qRgb(0, 255, 0));
    DDA(bg, QPoint(15, 555), QPoint(10, 560), qRgb(0, 255, 0));
    DDA(bg, QPoint(10, 10), QPoint(760, 10), qRgb(0, 255, 0));
    DDA(bg, QPoint(755, 5), QPoint(760, 10), qRgb(0, 255, 0));
    DDA(bg, QPoint(755, 15), QPoint(760, 10), qRgb(0, 255, 0));
}
